
<?php
	if(isset($_POST["Submit"]) && $_POST["Submit"] == "注册")
	{
		$user = $_POST["name"];
		$psw = $_POST["password"];
		$psw_confirm = $_POST["qrpassword"];
		if($user == "" || $psw == "" || $psw_confirm == "")
		{
			echo "<script>alert('必填项不能为空！'); history.go(-1);</script>";
		}
		else
		{
			if($psw == $psw_confirm)
			{
				$serverName='localhost';
				$userName='root';
				$password="123456";
				$dbName="gameshop";

				$conn=new mysqli($serverName,$userName,$password,$dbName);
				mysqli_set_charset($conn,'utf8');
				$sql = "select name from user where name = '$_POST[name]'";	//SQL语句
				$result = mysqli_query($conn,$sql);//执行SQL语句
				$num = mysqli_num_rows($result);	//统计执行结果影响的行数
				if($num)	//如果已经存在该用户
				{
					echo "该用户已存在，<a href='{{url('game/login')}}'>点击登录</a>";
				}
				else	//不存在当前注册用户名称
				{
				function removeSpaceEnter($str){
				$oldchar=array(" ","  ","\t","\n","\r");
				$newchar=array("","","","","");
				$str=str_replace($oldchar,$newchar,$str);
				return $str;
				}
				function lengthCheck($str){
				if(strlen($str)>10){
				return false;
				}
				return true;
				}
				function inputCheck($str){
				if(preg_match("/^[0-9A-Z_a-z_u0391-uFFE5]*$/",$str)){
				return true;
				}else {
				return false;
				}
				}
					
if(!lengthCheck($user)){
echo "用户名长度不能超过10";
return;
}
if(inputCheck($user)){
$pwd=md5($_POST["password"]);
$sql="select * from user where name='$user' and password = '$pwd'";
$result=mysqli_query($conn,$sql);

if($result->num_rows ==0){
$sql_insert = "insert into user (name,password,email) values('$_POST[name]',md5('$_POST[password]'),'$_POST[email]')";
$res_insert = mysqli_query($conn,$sql_insert);
if($res_insert){
	?>
注册用户成功！<a href="{{url('game/login')}}">点击跳转到登录界面</a>
<?php
}
else{
echo "密码与确认密码不匹配";
}
}
}else{
echo "用户名不能包含特殊字符，用户名只能包含数字，字母，中文";
}
}
}
}
		}			


					
	else
	{
		echo "<script>alert('提交未成功！'); history.go(-1);</script>";
	}
?>